
#include "Mailbox.h"
#include <iostream>

Mailbox::Mailbox(int buffer_size) {
	this->_buffer 		= new vector<Message*>();
	this->_buffer_size	= buffer_size;
}

Mailbox::~Mailbox()
{
	delete(this->_buffer);
}

void Mailbox::put_message(Message* message)
{
	this->_buffer->push_back(message);
}

// removes next message from _buffer and returns it:
Message* Mailbox::get_next_message()
{
	Message* ans = NULL;
	if ( !_buffer->empty() )
	{
		ans = _buffer->at(0);
		_buffer->erase(_buffer->begin());
	}
	//TODO: check if erasing first message causes to return deleted message
	//		because ans is pointer to message.
	return ans;
}

bool Mailbox::is_empty()
{
	return _buffer->empty();
}

bool Mailbox::is_full()
{
	return get_number_of_pending_messages() == this->_buffer_size;
}

int Mailbox::get_number_of_pending_messages()
{
	return _buffer->size();
}

void Mailbox::print()
{
	if (is_empty())
	{
		cout << "Empty" <<endl;
	}
	else
	{
		for (unsigned int i = 0; i < _buffer->size(); ++i) {
			((Message*)_buffer->at(i))->print_small();
			cout << " ";
		}
		cout << endl;
	}
}
